package com.midea.ai.air.ui.device.product.airconditioner.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.midea.ai.air.R;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.ui.device.product.utils.MetricsUtils;

/**
 * Created by ouyangdi on 2016/5/3.
 * 圆盘View
 */
public class DiskView extends View
{
    private Paint mPaintWhite, mPaintBg, mPaintYellow, mPaintPreVal; //画笔
    private float mCenterX, mCenterY;  //圆心坐标
    private Bitmap mBitmap;
    private float mRadiusInner, mRadiusOuter;
    private float mLineWidth;

    int LINESIZE = 6;
    int ROOMHUMISIZE = 10;
    int SIZE = 180;

    float radiu;
    float ang;
    int humi;

    public DiskView(Context context)
    {
        super(context);
        init(context);
    }

    public DiskView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public DiskView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        initData(context);
        initPaint();
        mBitmap = small(BitmapFactory.decodeResource(getResources(), R.drawable.ac_control_round_set));
    }

    private void initData(Context context) {
        Resources resources = context.getResources();
        mRadiusInner = resources.getDimension(R.dimen.height_230dp)/2f;
        mRadiusOuter = resources.getDimension(R.dimen.height_260dp)/2f;
        mLineWidth = resources.getDimension(R.dimen.height_1_5dp);
    }

    /**
     * 画笔初始化
     */
    private void initPaint() {
        Resources resources = getResources();

        //背景
        mPaintBg = new Paint();
        mPaintBg.setColor(resources.getColor(R.color.disk_paint_bg));
        mPaintBg.setStrokeWidth(mLineWidth);
        mPaintBg.setAntiAlias(true);

        //黄色画笔加粗表示当前室温
        mPaintPreVal = new Paint();
        mPaintPreVal.setColor(resources.getColor(R.color.disk_paint_yellow));
        mPaintPreVal.setStrokeWidth(Constant.toPix(ROOMHUMISIZE));
        mPaintPreVal.setAntiAlias(true);

        //白色画笔
        mPaintWhite = new Paint();
        mPaintWhite.setColor(resources.getColor(R.color.common_white));
        mPaintWhite.setStrokeWidth(mLineWidth);
        mPaintWhite.setAntiAlias(true);

        //黄色画笔
        mPaintYellow = new Paint();
        mPaintYellow.setColor(resources.getColor(R.color.disk_paint_yellow));
        mPaintYellow.setStrokeWidth(mLineWidth);
        mPaintYellow.setAntiAlias(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w/2f;
        mCenterY = h/2f;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        double a = 2 * Math.PI / SIZE;
        while (a <= 2 * Math.PI)
        {
            canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(a)), (float)(mCenterY + mRadiusInner * Math.sin(a)), (float)(mCenterX + mRadiusOuter * Math.cos(a)), (float)(mCenterY + mRadiusOuter * Math.sin(a)), mPaintBg);
            a = a + 2 * Math.PI / SIZE;
        }

        a = humi * Math.PI / 180.0;

        if (radiu >= 210)
        {
            a = 2 * Math.PI + a;
            if (a < (ang + 2 * Math.PI / SIZE))
            {
                while (a <= (ang + 2 * Math.PI / SIZE))
                {
                    canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(a)), (float)(mCenterY + mRadiusInner * Math.sin(a)), (float)(mCenterX + mRadiusOuter * Math.cos(a)), (float)(mCenterY + mRadiusOuter * Math.sin(a)), mPaintYellow);
                    a = a + 2 * Math.PI / SIZE;
                }
            }
            else
            {
                while (a >= (ang + 2 * Math.PI / SIZE))
                {
                    canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(a)), (float)(mCenterY + mRadiusInner * Math.sin(a)), (float)(mCenterX + mRadiusOuter * Math.cos(a)), (float)(mCenterY + mRadiusOuter * Math.sin(a)), mPaintWhite);
                    a = a - 2 * Math.PI / SIZE;
                }
            }
        }
        else
        {
            if (a < ang)
            {
                while ( a <= (ang + 2 * Math.PI / SIZE) && (Math.PI / 3  + 2 * Math.PI / SIZE) >= ang)
                {
                    canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(a)), (float)(mCenterY + mRadiusInner * Math.sin(a)), (float)(mCenterX + mRadiusOuter * Math.cos(a)), (float)(mCenterY + mRadiusOuter * Math.sin(a)), mPaintYellow);
                    a = a + 2 * Math.PI / SIZE;
                }
            }
            else
            {
                while (a >= (ang + 2 * Math.PI / SIZE) )
                {
                    canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(a)), (float)(mCenterY + mRadiusInner * Math.sin(a)), (float)(mCenterX + mRadiusOuter * Math.cos(a)), (float)(mCenterY + mRadiusOuter * Math.sin(a)), mPaintWhite);
                    a = a - 2 * Math.PI / SIZE;
                }
            }
        }

        canvas.drawLine((float)(mCenterX + mRadiusInner * Math.cos(humi * Math.PI / 180.0)), (float)(mCenterY + mRadiusInner * Math.sin(humi * Math.PI / 180.0)), (float)(mCenterX + mRadiusOuter * Math.cos(humi * Math.PI / 180.0)), (float)(mCenterY + mRadiusOuter * Math.sin(humi * Math.PI / 180.0)), mPaintPreVal);

        canvas.rotate(radiu - 2, mCenterX,  mCenterY);
        canvas.drawBitmap(mBitmap, mCenterX, (float) (mCenterY - (mRadiusOuter + 30)), null);
    }

    private Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        float y = bitmap.getHeight();
        float x = bitmap.getWidth();

        matrix.postScale((16.4f/14.6f)*MetricsUtils.Dp2Px(getContext(), 40)/ y, (16.4f/14.6f)*MetricsUtils.Dp2Px(getContext(), 40)/ y); // 长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    public void doAction(float x, float y,int west,int setHumi)
    {
        humi = (west - 60) * 6 - 90;
        if (setHumi == 0)
        {
            radiu = (float) (((float) Math.atan2( y - (getHeight() / 2), x - (mCenterX)) * (float)180 / Math.PI) + 90);
        }
        else
        {
            if (setHumi >= 45)
            {
                radiu = 150 + 6 * (setHumi - 85);
            }
            else
            {
                radiu = 210 + 6 * (setHumi - 35);
            }
        }
        ang = (float) ((radiu - 90) / 180.0 * Math.PI);
        invalidate();
    }
}